package com.nhn.productmyself.lib.common;

import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.text.TextUtils;

import com.nhn.productmyself.lib.FacebookLoginUsingFragmentActivity;
import com.nhn.productmyself.lib.ValueContainer;

public class ShareMessageUtils {

	public static final String TAG = ShareMessageUtils.class.getSimpleName();

	/**
	 * Send message via Viber.
	 * 
	 * @param context
	 *            the context
	 * @return true, if successful
	 */
	public static boolean sendMessageViaViber(Context context, String messageContent) {
		boolean found = false;
		Intent share = new Intent(android.content.Intent.ACTION_SEND);
		share.setType("text/plain");

		// gets the list of intents that can be loaded.
		List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(share, 0);
		if (!resInfo.isEmpty()) {
			for (ResolveInfo info : resInfo) {
				if (info.activityInfo.packageName.toLowerCase(Locale.getDefault()).contains("com.viber.voip")
						|| info.activityInfo.name.toLowerCase(Locale.getDefault()).contains("com.viber.voip")) {
					share.putExtra(Intent.EXTRA_TEXT, messageContent);
					share.setPackage(info.activityInfo.packageName);
					found = true;
					context.startActivity(Intent.createChooser(share, "Select"));
					break;
				}
			}
		}
		return found;
	}

	/**
	 * Send message via SMS.
	 * 
	 * @param context
	 *            the context
	 * @param phoneNumber
	 *            the phone number
	 * @param message
	 *            the message
	 */
	public static void sendMessageViaSMS(final Context context, String phoneNumber, String message) {
		Intent sendIntent = new Intent(Intent.ACTION_VIEW);
		if (!TextUtils.isEmpty(phoneNumber)) {
			sendIntent.putExtra("address", phoneNumber);
		}
		if (!TextUtils.isEmpty(message)) {
			sendIntent.putExtra("sms_body", message); 
		}
		sendIntent.setType("vnd.android-dir/mms-sms");
		context.startActivity(sendIntent);
	}

	/**
	 * Send message via GMAIL.
	 * 
	 * @param context
	 *            the context
	 * @param address
	 *            the address
	 * @param subject
	 *            the subject
	 * @param body
	 *            the body
	 */
	public static void sendMessageViaGmail(Context context, String[] address, String subject, String body) {

		Intent intentEmail = new Intent(Intent.ACTION_SEND);
		if (address != null && address.length > 0) {
			for (int i = 0; i < address.length; i++) {
				// intentEmail.putExtra(Intent.EXTRA_EMAIL, new String[] {
				// "your.email@gmail.com" });
				intentEmail.putExtra(Intent.EXTRA_EMAIL, address);
			}
		}
		if (!TextUtils.isEmpty(subject)) {
			intentEmail.putExtra(Intent.EXTRA_SUBJECT, subject);
		}
		if (!TextUtils.isEmpty(body)) {
			intentEmail.putExtra(Intent.EXTRA_TEXT, body);
		}
		intentEmail.setType("message/rfc822");
		context.startActivity(Intent.createChooser(intentEmail, "Choose an email provider :"));
	}

	/**
	 * Send message via facebook.
	 *
	 * @param context the context
	 * @param caption the caption
	 * @param message the message
	 */
	public static void sendMessageViaFacebook(final Context context, String caption, String message) {
		ValueContainer.getInstance().setMessageCaption(caption);
		ValueContainer.getInstance().setMessageName(caption);
		ValueContainer.getInstance().setMessageContent(message);
		ValueContainer.getInstance().setMessageDescription(message);
		context.startActivity(new Intent(context, FacebookLoginUsingFragmentActivity.class));
	}
	
	
	
}
